package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/15 12:10 上午
 */
public class 剑指_Offer_18_删除链表的节点 {

    /**
     * 链表处理--删除链表节点
     * @param head
     * @param val
     * @return
     */
    public ListNode deleteNode(ListNode head, int val) {
        ListNode preHead = new ListNode(val + 1);
        preHead.next = head;
        ListNode temp = preHead, pre = preHead;
        while (temp.next != null) {
            if (temp.val == val) {
                pre.next = temp.next;
            } else {
                pre = temp;
            }
            temp = temp.next;
        }
        if (temp.val == val) {
            pre.next = temp.next;
        }
        return preHead.next;
    }
}
